﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CE.Provider;
using CE.Global;

public partial class Provider_CarInsurance_ProviderCarMake : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCICMAK";
    protected void Page_Load(object sender, EventArgs e)
    {
        //usrLink.PageName = "Provider Car Make";
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!Page.IsPostBack)
        {

            DataTable dtCarMake = General.GetComboItem("CarMake", " [Description]", true);
            ddlCXCarMake.DataSource = dtCarMake;
            ddlCXCarMake.DataTextField = dtCarMake.Columns[0].ColumnName.ToString();
            ddlCXCarMake.DataValueField = dtCarMake.Columns[1].ColumnName.ToString();
            ddlCXCarMake.DataBind();
            ddlCXCarMake.Items.Insert(0, new ListItem("Select", "-1"));
            dtCarMake = null;

            if (Request.Params["CarMakeId"] != null)
            {
                int _carmakeid = 0;
                if (int.TryParse(Request.Params["CarMakeId"].ToString(), out _carmakeid))
                {
                    ddlCXCarMake.Text = _carmakeid.ToString();
                    ShowCarMake(_carmakeid, Convert.ToInt64(Session[General.ProviderCompanyId].ToString()));
                }
            }
        }
    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {
                CarMake _carmake = new CarMake();
                CarMakeDB _carmakedb = new CarMakeDB();
                int _trantype = 1;
                _carmake.ProviderCarMakeId = CheckCarMake(Convert.ToInt32(ddlCXCarMake.Text), Convert.ToInt64(Session[General.ProviderCompanyId].ToString()));
                if(_carmake.ProviderCarMakeId > 0)
                {
                    _trantype = 2;
                }
                _carmake.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
                _carmake.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());

                _carmake.CarMakeId = Convert.ToInt32(ddlCXCarMake.Text);
                _carmake.Description = txtMyCarMakeDescription.Text;
                _carmake.Status = chkStatus.Checked;

                _carmake.ProviderCarMakeId = _carmakedb.Insert(_carmake);
                if (_carmake.ProviderCarMakeId > 0)
                {
                    InsertProviderAuthTransaction(_carmake.ProviderCarMakeId, "CICarMake", _trantype, 100, _authcode, _carmake.ProviderCompanyId, _carmake.RefCEUserId);
                    lblMessage.Text = "Updated successfully.";
                    Response.Redirect("~/Provider/CarInsurance/ViewProviderCarMake.aspx");
                }
                else
                {
                    lblMessage.Text = "Updating failed.";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    protected void ShowCarMake(int CarMakeId, Int64 ProviderCompanyId)
    {
        CarMake _carmake = new CarMake();
        CarMakeDB _carmakedb = new CarMakeDB();

        _carmake = _carmakedb.GetProviderCarMake(CarMakeId, ProviderCompanyId);

        if (_carmake != null)
        {
            txtMyCarMakeDescription.Text = _carmake.Description;
            chkStatus.Checked = _carmake.Status;
        }
        else
        {
            ClearFields();
        }
    }

    protected Int32 CheckCarMake(int CarMakeId, Int64 ProviderCompanyId)
    {
        CarMake _carmake = new CarMake();
        CarMakeDB _carmakedb = new CarMakeDB();
        Int32 _retValue = 0;
        _carmake = _carmakedb.GetProviderCarMake(CarMakeId, ProviderCompanyId);

        if (_carmake != null)
        {
            _retValue = _carmake.ProviderCarMakeId;
        }

        return _retValue;
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Int32 _carmakeid = Convert.ToInt32(ddlCXCarMake.Text);
        Int64 _providercompanyid = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
        string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
        Int64 _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());
        if (CheckAuthCode(_authcode, 100))
        {
            if (_carmakeid != 0)
            {
                CarMakeDB _carmakedb = new CarMakeDB();
                CarMake _carmake = _carmakedb.GetProviderCarMake(_carmakeid, _providercompanyid);
                if (_carmake != null)
                {
                    if (_carmakedb.Delete(_carmakeid, _providercompanyid))
                    {
                        InsertProviderAuthTransaction(_carmake.ProviderCarMakeId, "CICarMake", 3, 100, _authcode, _carmake.ProviderCompanyId, _refceuserid);
                        Response.Redirect("ViewProviderCarMake.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Deleting failed.";
                    }
                }
                else
                {
                    lblMessage.Text = "No such car make to delete.";
                }
            }
        }
        else
        {
            lblMessage.Text = "Wrong auth code!";
        }
    }

    private void ClearFields()
    {
        txtMyCarMakeDescription.Text = "";
        chkStatus.Checked = false;
        lblMessage.Text = "";
    }

    protected void ddlCXCarMake_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowCarMake(Convert.ToInt32(ddlCXCarMake.Text), Convert.ToInt64(Session[General.ProviderCompanyId].ToString()));
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderCarMake.aspx");
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        ddlCXCarMake.Text = "-1";
        ClearFields();
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType, 
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            btnNew.Enabled = false;
            ibtSubmit.Enabled = false;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            btnNew.Enabled = true;
            ibtSubmit.Enabled = true;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnNew.Enabled = true;
            ibtSubmit.Enabled = true;
            btnDelete.Enabled = true;
        }
    }

}
